package com.xiaohao.server.parkmap.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohao.server.parkmap.entity.Customer;
import com.xiaohao.server.parkmap.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    public Customer queryById(Integer id) {
        return customerMapper.selectById(id);
    }

    public Customer queryOne(Customer customer) {
        return customerMapper.selectOne(new QueryWrapper<>(customer));
    }

    public List<Customer> query(Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper =
            new LambdaQueryWrapper<>(customer);
        return customerMapper.selectList(queryWrapper);
    }

    public Integer delete(Integer id) {
        return customerMapper.deleteById(id);
    }

    public Integer save(Customer customer) {
        if (Objects.nonNull(customer) && Objects.nonNull(customer.getId())) {
            return customerMapper.updateById(customer);
        } else {
            customer.setReward(0);
            return customerMapper.insert(customer);
        }
    }
}
